const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fadeInText: true,
    fadeInShareText:true,
    domeName:'',
    hopsName:'',
    hopsImage:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.share){
      this.setData({
        share:1
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.hopsDetail();
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },
  // 活动详情获取
  hopsDetail:function(){
    var that = this;
    api.POST({
      params:{},
      url: app.globalData.base_url + 'hops/index',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          that.setData({
            hopsDetail: res.data.data.result
          })
        }

      },
      fail: function (fail) {
        util.showError("../../../", "获取失败，请检查网络");
      }
    })
  },
  // 签到
  signIn:function(){
    var that = this;
    // console.log(that.data.hopsDetail.is_sign);
    if (that.data.hopsDetail.is_sign==1){
      util.showError("今日已签到成功,请勿重复操作");
      return ;
    }
    api.POST({
      params: {},
      url: app.globalData.base_url + 'hops/sign',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          that.setData({
            domeName: "签到成功",
            hopsName: res.data.data.hops_name,
            hopsImage: res.data.data.list_img,
          })
          that.hopsDetail();
          that.fadeIn();
          that.fadeInShare();
        }else{
          util.showError(res.data.message);
        }

      },
      fail: function (fail) {
        util.showError("../../../", "获取失败，请检查网络");
      }
    })
  },
  // 分享后请求
  sharehops:function(){
    var that = this;
    if (that.data.hopsDetail.is_share == 1) {
      util.showError("分享成功");
      return;
    }
    api.POST({
      params: {},
      url: app.globalData.base_url + 'hops/shareGet',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          that.setData({
            domeName: "分享成功",
            hopsName: res.data.data.hops_name,
            hopsImage: res.data.data.list_img,
          })
          that.hopsDetail();
          that.fadeIn();
          that.fadeInShare();
        } else {
          util.showError(res.data.message);
        }

      },
      fail: function (fail) {
        util.showError("../../../", "获取失败，请检查网络");
      }
    })
  },
  // 分享事件
  onShareAppMessage: function (res) {
    var that = this;
    var img = that.data.hopsDetail.share_img;
    var url = '/pages/hops/hops?share=1'
    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: "攒酒花  得好酒",
        path: url,
        imageUrl: "",
        success: function (res) {
          that.sharehops();
        },
        fail: function (res) {
          // 转发失败
        }
      }
    }
  },
  // 淡入
  fadeIn: function () {
    var that = this;
    var fadeIn = wx.createAnimation({
      duration: 500
    })
    that.setData({
      fadeInText: false
    })
    fadeIn.opacity(1).step();
    that.setData({
      animationData: fadeIn.export(),
      shareData: fadeIn.export()
    })
  },
  // 淡出
  fadeOut: function () {
    var that = this;
    var fadeOut = wx.createAnimation({
      duration: 500
    })
    fadeOut.opacity(0).step();
    that.setData({
      animationData: fadeOut.export(),
      shareData: fadeOut.export(),
    })
    let timer = setTimeout(function () {
      that.setData({
        fadeInText: true
      })
      clearTimeout(timer);
    }, 500)
  },

  //分享弹窗淡入
  fadeInShare: function () {
    var that = this;
    var fadeInShare = wx.createAnimation({
      duration: 500
    })
    that.setData({
      fadeInShareText: false
    })
    fadeInShare.opacity(1).step();
    that.setData({
      shareData: fadeInShare.export()
    })
  },
  // 分享弹窗淡出
  fadeOutShare: function () {
    var that = this;
    var fadeOutShare = wx.createAnimation({
      duration: 500
    })
    fadeOutShare.opacity(0).step();
    that.setData({
      shareData: fadeOutShare.export(),
    })
    let timer = setTimeout(function () {
      that.setData({
        fadeInShareText: true
      })
      clearTimeout(timer);
    }, 500)
  },

  // 显示操作按钮
  intoSelect: function (e) {
    this.fadeIn();
    this.fadeInShare();
  },
  // 隐藏操作按钮
  outSelect: function () {
    this.fadeOut();
    this.fadeOutShare();
  },
  goLink:function(){
    wx.switchTab({
      url: '../index/index'
    });
  },
  winLink:function(){
    app.globalData.winType = 2;
    wx.switchTab({
      url: '../wineNew/wineNew'
    });
  },
  goBuyLink:function(){
    wx.navigateTo({
      url: '../hopsBuy/hopsBuy'
    })
  },
  prompt:function(){
    util.showError('我们将尽快推出酒花，敬请期待');
  }
  
})